package com.tian.photopicker.ui;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tian.photopicker.R;
import com.tian.photopicker.adapter.PhotoPagerAdapter;
import com.tian.photopicker.adapter.PickerGridAdapter;
import com.tian.photopicker.bean.Photo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 图片预览
 * @author tian
 */
public class PhotoPreviewFragment extends Fragment {

    public final static String PHOTO_PATHS = "photo_paths";
    public final static String PHOTO_INDEX = "photo_index";
    public final static String SELECT_PHOTO_COUNT = "select_photo_count";
    public final static String MAX_COUNT = "max_count";
    private List<String> photoList;
    private ViewPager viewPager;
    private int currentIndex;
    private PhotoPagerAdapter pagerAdapter;
    private int photoTotal;
    private TextView tvChose;
    private OnUpdateSelectInfo mOnUpdateSelectInfo;
    private int selectCount;
    private int maxCount;
    public static PhotoPreviewFragment newInstance(List<String> photos, int index,int selectCount,int max) {
        PhotoPreviewFragment fragment = new PhotoPreviewFragment();
        Bundle mBundle = new Bundle();
        mBundle.putSerializable(PHOTO_PATHS, (Serializable) photos);
        mBundle.putInt(PHOTO_INDEX,index);
        mBundle.putInt(SELECT_PHOTO_COUNT,selectCount);
        mBundle.putInt(MAX_COUNT,max);
        fragment.setArguments(mBundle);
        return fragment;
    }

    public void setmOnUpdateSelectInfo(OnUpdateSelectInfo mOnUpdateSelectInfo) {
        this.mOnUpdateSelectInfo = mOnUpdateSelectInfo;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        photoList = new ArrayList<>();
        Bundle bundle = getArguments();
        if (bundle != null) {
            photoList.clear();
            photoList = (List<String>) bundle.getSerializable(PHOTO_PATHS);
            currentIndex = bundle.getInt(PHOTO_INDEX);
            selectCount = bundle.getInt(SELECT_PHOTO_COUNT);
            maxCount = bundle.getInt(MAX_COUNT);
        }
        photoTotal = photoList.size();
        pagerAdapter = new PhotoPagerAdapter(getActivity(),photoList);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_photo_preview, container, false);
        initComponent(view);
        return view;
    }

    private void initComponent(View view) {
        ImageView ivBack = (ImageView) view.findViewById(R.id.iv_pick_back);
        final TextView tvIndex = (TextView) view.findViewById(R.id.tv_index);
        tvChose = (TextView) view.findViewById(R.id.tv_pre_chose);
        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        viewPager.setOffscreenPageLimit(4);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(currentIndex);
        tvIndex.setText((currentIndex+1)+"/"+ photoTotal);
        isSelected(currentIndex);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getActivity().getSupportFragmentManager().getBackStackEntryCount() > 0) {
                    getActivity().getSupportFragmentManager().popBackStack();

                }

            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                currentIndex = position;
                tvIndex.setText((position+1)+"/"+ photoTotal);
                isSelected(position);
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        tvChose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PickerGridAdapter adapter = ((PhotoPickerActivity)getActivity()).getGridAdapter();
                if (adapter.isSelectedPath(photoList.get(currentIndex))) {
                    tvChose.setSelected(false);
                    selectCount -- ;
                    adapter.getSelectedPhotos()
                            .remove(photoList.get(currentIndex));
                } else {
                    if (selectCount >= maxCount) {
                        Toast.makeText(getActivity(), "您最多只能选择"+maxCount
                                +"张图片", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    tvChose.setSelected(true);
                    selectCount ++ ;
                    ((PhotoPickerActivity)getActivity()).getGridAdapter().getSelectedPhotos()
                            .add(photoList.get(currentIndex));
                }

            }
        });

    }
    public void isSelected (int index) {
        PickerGridAdapter adapter = ((PhotoPickerActivity)getActivity()).getGridAdapter();
        if (adapter.isSelectedPath(photoList.get(index))) {
            tvChose.setSelected(true);
        } else {
            tvChose.setSelected(false);
        }
    }
    public interface OnUpdateSelectInfo {
        /**
         * 更新
         * @param selectCount
         */
        void updateStatus (int selectCount);
    }

    @Override
    public void onDestroyView() {
        if (mOnUpdateSelectInfo != null) {
            mOnUpdateSelectInfo.updateStatus(selectCount);
        }
        super.onDestroyView();

    }
}
